package com.study.rabbitmq2.routing;

import com.rabbitmq.client.*;
import com.study.rabbitmq2.util.ConnectionUtil;

import java.io.IOException;

/**
 * @author chenyeqing
 */
public class Recv2 {
    /**
     * 短信队列
     */
    private final static String QUEUE_NAME = "direct_exchange_queue_email";
    private final static String EXCHANGE_NAME = "test_direct_exchange";

    public static void main(String[] args) throws IOException {

        Connection connection = ConnectionUtil.getConnection();

        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        // 绑定队列到交换机，同时指定需要订阅的routing key，可以指定多个
        // 指定接收方发送方指定routing key为sms的消息
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "email");

        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body);
                System.out.println("[邮件服务] received:" + msg);
            }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}